<?php

namespace App\Http\Controllers\outline;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\postscheck;
use App\Http\Requests\userscheck;


class ViewTheArticle extends Controller
{
    //
    public function SpecialColumn()
    {

        $data = DB::table('columns')->select('column_name')->get();
        if ($data != null) {
            return response()->success(200, "成功", $data);
        } else {
            return response()->fail(100, '失败');
        }


    }

    public function ConterntMessage(Request $request)
    {
    	$column_id=$request->column_id;
        $data = DB::table('users as U')->join('posts as P', 'U.id', 'P.user_id')->select('body', 'title', 'like', 'view', 'P.created_at', 'user_name','P.id')->where('column_id',$column_id)->paginate(3);
        if ($data != null) {
            return response()->success(200, "成功", $data);
        } else {
            return response()->fail(100, '失败');
        }
    }

    public function getColumnMessage(userscheck $request)
    {
    
        $id=$request->id;
        $data=DB::table('users as U' )->join('posts as P','U.id', 'P.user_id')
        ->join('tags as t','U.id','t.id')
        ->where('P.id',$id)
        ->select('P.id','user_name','body','title','t.created_at','P.column_id','like','view','t.tag_name')
        ->get();
        if($data!=null){
            return response()->success(200,"成功",$data);
        }
        else{
            return response()->fail(100,'失败');
        }
    }
    public function Thumbs(postscheck $request)
    {
        $pull_id = $request->pull_id;
        $push_id = $request->push_id;
        $post_id = $request->post_id;
        if ($request->isMethod('post')) {
            $msg = DB::table('messages')->where('push_id', $push_id)->value('msg');
            $data = DB::table('posts as p')->join('post_tags as t', 'p.id', 't.post_id')->where('p.id', $post_id)->increment('like');
            $db = DB::table('posts as p')->join('post_tags as t', 'p.id', 't.post_id')->where('p.id', $post_id)->value('like');
            if ($data != null) {
                if ($db != 0 && $db % 10 == 0) {
                    DB::table('users as u')->join('posts as p', 'u.id', 'p.user_id')->where('user_id', $pull_id)->increment('rating', 2);
                    $r = DB::table('messages as m')->join('posts as p', 'm.id', 'p.user_id');
                    $result = $r->where('pull_id', $pull_id)->update(['msg' => "您的点赞满了{$db}积分加2"]);
                }
                return response()->success(200, "成功", $msg);
            } else {
                return response()->fail(100, '失败');
            }
        }
    }

    public function CancelThumbs(postscheck $request)
    {
        $pull_id = $request->pull_id;
        $push_id = $request->push_id;
        $post_id = $request->post_id;
        if ($request->isMethod('post')) {
            $msg = DB::table('messages')->where('push_id', $push_id)->delete();
            $data = DB::table('posts as p')->join('post_tags as t', 'p.id', 't.post_id')->where('p.id', $post_id)->decrement('like');
            $db = DB::table('posts as p')->join('post_tags as t', 'p.id', 't.post_id')->where('p.id', $post_id)->value('like');

            if ($data != null) {
                if ($db != 0 && ($db + 1) % 10 == 0) {
                    DB::table('users as u')->join('posts as p', 'u.id', 'p.user_id')->where('user_id', $pull_id)->decrement('rating', 2);
                    $msg = DB::table('messages as m')->join('posts as p', 'm.id', 'p.user_id');
                    $result = $msg->where('pull_id', $pull_id)->delete();
                }
                $r = "取消赞成功";

                return response()->success(200, "成功", $r);
            } else {
                return response()->fail(100, '失败');
            }
        }
    }


    public function getHotContents()//获取热点文章
    {

        $data=DB::table('users as U' )
        ->join('posts as P','U.id', 'P.user_id')
        ->select('P.id','body','title','like','view','P.created_at','user_name')
        ->orderby('hot','desc')->paginate(5);
        if($data!=null){
            return response()->success(200,"成功",$data);
        }
        else{
            return response()->fail(100,'失败');
        }

    }

    public function getNewContents()//获取最新文章
    {

        $data=DB::table('users as U' )
        ->join('posts as P','U.id', 'P.user_id')
        ->select('P.id','body','title','like','view','P.created_at','user_name')
        ->orderby('created_at','desc')->paginate(5);
        if($data!=null){
            return response()->success(200,"成功",$data);
        }
        else{
            return response()->fail(100,'失败');
        }

    }

}
